package metrics

import "sync"

var (
	defaultRegistry Registry
	once            sync.Once
)

type nilRegistry struct{}

func (n nilRegistry) CreateGauge(opts GaugeOpts) error {
	return nil
}

func (n nilRegistry) CreateCounter(opts CounterOpts) error {
	return nil
}

func (n nilRegistry) CreateSummary(opts SummaryOpts) error {
	return nil
}

func (n nilRegistry) CreateHistogram(opts HistogramOpts) error {
	return nil
}

func (n nilRegistry) GaugeSet(name string, val float64, labels map[string]string) error {
	return nil
}

func (n nilRegistry) CounterAdd(name string, val float64, labels map[string]string) error {
	return nil
}

func (n nilRegistry) SummaryObserve(name string, val float64, Labels map[string]string) error {
	return nil
}

func (n nilRegistry) HistogramObserve(name string, val float64, labels map[string]string) error {
	return nil
}

func InitRegistry() {
	once.Do(func() {
		if defaultRegistry == nil {
			defaultRegistry = nilRegistry{}
		}
	})
}

func SetGlobalRegistry(r Registry) {
	once.Do(func() {
		defaultRegistry = r
	})
}

//CreateGauge init a new gauge type
func CreateGauge(opts GaugeOpts) error {
	return defaultRegistry.CreateGauge(opts)
}

//CreateCounter init a new counter type
func CreateCounter(opts CounterOpts) error {
	return defaultRegistry.CreateCounter(opts)
}

//CreateSummary init a new summary type
func CreateSummary(opts SummaryOpts) error {
	return defaultRegistry.CreateSummary(opts)
}

//CreateHistogram init a new summary type
func CreateHistogram(opts HistogramOpts) error {
	return defaultRegistry.CreateHistogram(opts)
}

//GaugeSet set a new value to a collector
func GaugeSet(name string, val float64, labels map[string]string) error {
	return defaultRegistry.GaugeSet(name, val, labels)
}

//CounterAdd increase value of a collector
func CounterAdd(name string, val float64, labels map[string]string) error {
	return defaultRegistry.CounterAdd(name, val, labels)
}

//SummaryObserve gives a value to summary collector
func SummaryObserve(name string, val float64, labels map[string]string) error {
	return defaultRegistry.SummaryObserve(name, val, labels)
}

//HistogramObserve gives a value to histogram collector
func HistogramObserve(name string, val float64, labels map[string]string) error {
	return defaultRegistry.HistogramObserve(name, val, labels)
}
